package android.money;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.money.data.Account;
import android.money.data.Card;
import android.money.db.AccountManager;
import android.money.db.DBInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AccountEdit extends AccountEditor {

	private int id;

	private Cursor accountInfo;

	private AccountManager accManager;

	private String expireMSG = "Your account was expired. "
			+ "Please re-enter the information or delete the account";

	private String configmMSG = "Really want to delete this account?";

	private String TXT_SURE = "Sure";

	private String TXT_NO = "Cancel";

	private String TXT_SUCCESS_DELETE = "Account Deleted!";

	private String TXT_FAIL_DELETE = "Deleting Account Fail!";

	private String TXT_SUCCESS_EDIT = "Account Edited!";

	private String TXT_FAIL_EDIT = "Editing Account Fail!";

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		id = getIntent().getExtras().getInt(DBInfo.account_id);
		accManager = new AccountManager(this);
		accManager.open();
		accountInfo = accManager.fetchAccount(id);
		populateFields();
		submitButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				if (!isValidData())
					return;
				Account account = new Account();
				account.setNumber(cardNumber.getText().toString());
				account.setSec_code(secCode.getText().toString());
				account
						.setType(new Card(typeSpinner.getSelectedItemPosition()));
				short expMonth = Short.parseShort((String) expireMonthSpinner
						.getSelectedItem());
				short expYear = Short.parseShort((String) expireYearSpinner
						.getSelectedItem());
				account.setExpire_month(expMonth);
				account.setExpire_year(expYear);
				account.setName(accName.getText().toString());
				account.setHolderName(holderName.getText().toString());

				if (accManager.updateAccount(id, account)) {
					Toast.makeText(AccountEdit.this, TXT_SUCCESS_EDIT,
							Toast.LENGTH_SHORT).show();
					finish();
				} else {
					Toast.makeText(AccountEdit.this, TXT_FAIL_EDIT,
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		deleteButton.setOnClickListener(new Button.OnClickListener() {
			OnClickListener confirmDelete = new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case DialogInterface.BUTTON1:
						if (accManager.deleteAccount(id))
							Toast.makeText(AccountEdit.this,
									TXT_SUCCESS_DELETE, Toast.LENGTH_SHORT)
									.show();
						else
							Toast.makeText(AccountEdit.this, TXT_FAIL_DELETE,
									Toast.LENGTH_SHORT).show();
						finish();
						break;
					}
				}
			};

			public void onClick(View arg0) {
				/*AlertDialog.show(AccountEdit.this, null,
						android.R.drawable.ic_input_delete, configmMSG,
						TXT_SURE, confirmDelete, TXT_NO, confirmDelete, false,
						null);
				 */
				AlertDialog confirmDeleteDialog = new AlertDialog.Builder(AccountEdit.this).create();
				confirmDeleteDialog.setIcon(android.R.drawable.ic_input_delete);
				confirmDeleteDialog.setTitle(configmMSG);
				confirmDeleteDialog.setButton(TXT_SURE, confirmDelete);
				confirmDeleteDialog.setButton2(TXT_NO, confirmDelete);
				confirmDeleteDialog.show();

			}
		});
	}

	private void populateFields() {
		startManagingCursor(accountInfo);
		cardNumber.setText(accountInfo.getString(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_number)));
		secCode.setText(accountInfo.getString(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_sec_code)));
		typeSpinner.setSelection(accountInfo.getInt(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_type)));
		int expYear = accountInfo.getInt(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_expire_year));
		int expMonth = accountInfo.getInt(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_expire_month));
		if (wasExpired(expYear, expMonth)) {
			Toast.makeText(this, expireMSG, Toast.LENGTH_SHORT).show();
		} else {
			expireMonthSpinner.setSelection(expMonth - 1);
			expireYearSpinner.setSelection(getYearList().indexOf("" + expYear));
		}
		accName.setText(accountInfo.getString(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_name)));
		holderName.setText(accountInfo.getString(accountInfo
				.getColumnIndexOrThrow(DBInfo.account_holder_name)));
	}
}
